package com.usian.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.EmpMapper;
import com.usian.mapper.PostsMapper;
import com.usian.pojo.Emp;
import com.usian.pojo.Posts;
import com.usian.service.PostsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class PostsServiceImpl implements PostsService {
    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private EmpMapper empMapper;

    //添加
    @Override
    public void add(Posts posts,HttpSession session) {
        Integer id=(Integer) session.getAttribute("login");
        Emp emp=empMapper.findById(id);
        if (emp.getGrade()==0){
            posts.setCode(posts.getCode()-1);
        }
        postsMapper.insert(posts);
    }

    //查询
    @Override
    public Page<Posts> findPage(HttpSession session,Integer page, Integer pageSize, String name) {
        Integer id=(Integer) session.getAttribute("login");
        Emp emp=postsMapper.findById(id);
        Integer code = emp.getGrade();
        Integer did = emp.getDid();
        Page<Posts> page1=new Page<>(page,pageSize);
        if (code==0){
            Page<Posts> page2=postsMapper.findPage1(page1,name,code,did,id);
            return page2;
        }
        Page<Posts> page3=postsMapper.findPage2(page1,name,code,did,id);
        return page3;
    }

    //查询集合
    @Override
    public List<Posts> findByList(Integer did) {
        List<Posts> list=postsMapper.findByList(did);
        return list;
    }

    //回显
    @Override
    public Posts findById(Integer id) {
        return postsMapper.selectById(id);
    }
}
















